package com.boyinet.imesh2json.viewmodel

import com.boyinet.imesh2json.bean.Template
import com.boyinet.imesh2json.model.TemplateModel
import com.boyinet.imesh2json.repo.TemplateRepository
import javafx.beans.property.BooleanProperty
import javafx.beans.property.SimpleBooleanProperty
import javafx.collections.FXCollections
import javafx.collections.ObservableList
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component

@Component
class TemplateViewModel {


    val log: Logger = LoggerFactory.getLogger(TemplateModel::class.java)

    @Autowired
    lateinit var templateRepository: TemplateRepository

    @Autowired
    lateinit var dataMapsViewModel: DataMapsViewModel

    var current: TemplateModel = TemplateModel()

    var original: TemplateModel = TemplateModel()

    var disableDel: BooleanProperty = SimpleBooleanProperty(false)

    var disableOk: BooleanProperty = SimpleBooleanProperty(false)

    var disableApply: BooleanProperty = SimpleBooleanProperty(false)

    var originalIndex = -1

    val lists: ObservableList<Template> by lazy {
        val toList = templateRepository.findAll().toList()
        disable(toList.isEmpty())
        FXCollections.observableArrayList(toList)
    }

    fun showInfo(index: Int) {
        val show = lists[index]
        original.fill(show)
        originalIndex = index
        current.fill(show)
        dataMapsViewModel.show(current.id.value)
    }

    fun insert() {
        current.init()
        val insert = current.export()
        lists.add(insert)
        templateRepository.save(insert)
        disable(false)
    }

    private fun disable(status: Boolean) {
        disableDel.set(status)
        disableOk.set(status)
        disableApply.set(status)
    }


    fun update(index: Int) {
        lists[index] = current.export()
    }

    fun delete() {
        val delete = current.export()
        lists.remove(delete)
        log.info("[TemplateViewModel Delete],template info : $delete")
        templateRepository.delete(delete)
        dataMapsViewModel.deleteAll(delete.id)
        original.isDelete = true
        disable(lists.isEmpty())
    }

    fun save() {
        val save = current.export()
        templateRepository.save(save)
        dataMapsViewModel.saveAll()
        lists[originalIndex] = save
    }

    fun revert() {
        if (!original.isDelete && originalIndex != -1) {
            lists[originalIndex] = original.export()
            showInfo(originalIndex)
        }
    }

    fun showEmpty() {
        current.fill(Template(-1).apply {
            name = ""
            sn = ""
            signalStrength = ""
            powerSource = ""
            powerSupply = ""
            batteryPower = ""
            alarmStatus = ""
            originalValue = false

            unit = ""
            sensorType = ""
            fields = ""
        })
        dataMapsViewModel.show(current.id.value)
    }
}